package org.brisling.oa.service;

import java.util.List;
import java.util.Map;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.oa.domain.OALogisticsRepair;
import org.brisling.oa.domain.OALogisticsSmallRepair;
import org.brisling.oa.queryService.QueryOALogisticsRepairService;
import org.brisling.oa.repository.OALogisticsRepairRepo;
import org.brisling.oa.service.base.BaseOaFlowService;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.brisling.workflow.domain.VOHistoric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/oaLogisticsRepair")
/**
 * 
 * <p>
 * Title: OALogisticsRepairService
 * </p>
 * <p>
 * Description: 后勤设备采购服务类
 * </p>
 * <p>
 * Company: 长沙东勤信息科技发展有限公司
 * </p>
 * 
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年7月25日 上午11:05:29
 */
public class OALogisticsRepairService extends BaseOaFlowService<OALogisticsRepair> {

	@SuppressWarnings("unused")
	@Autowired
	private OALogisticsRepairRepo baseFlowRepo;

	@Autowired
	private QueryDictionaryService dicService;

	@Autowired
	private ShiroSecurityService shiroSecurityService;

	public OALogisticsRepairService() {
		this.clazz = OALogisticsRepair.class;
		this.moduleName = ConstSysParam.MODULE_OA_LOGISTICS_REPAIR;
	}

	/**
	 * 获取业务主页面
	 * 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage() {
		return getBizMainPage();
	}

	/**
	 * 获取业务编辑页面
	 * 
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage() {
		return getBizEditPage();
	}

	/**
	 * 返回缺陷记录流程实例id的执行记录
	 * 
	 * @param procInstId
	 *            流程实例id
	 * @return List<HistoricTaskInstance> 流程实例执行记录
	 */
	@RequestMapping("/flowHistorics.action")
	public List<VOHistoric> getHistoricByProcInstId(
			@RequestParam(value = "flowInstId", required = false) String procInstId) {
		return super.getHistoricByProcInstId(procInstId);
	}

	/**
	 * 返回缺陷记录流程实例id的执行记录
	 * 
	 * @param procInstId
	 *            流程实例id
	 * @return List<HistoricTaskInstance> 流程实例执行记录
	 */
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<VOHistoric> getBizHistoricListDomainByProcInstId(
			@RequestParam(value = "flowInstId", required = false) String procInstId) {
		return super.getHistoricListDomainByProcInstId(procInstId);
	}

	/**
	 * 分页查询用户列表页面
	 * 
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/flowListDomain.action")
	public ListDomain<OALogisticsRepair> getListDomain(@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "rows", required = false) Integer rows,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "flowtype", required = false) String flowtype,
			@RequestParam(value = "fullBizcode", required = false) String fullBizcode) {

		if (flowtype == null)
			return super.getListDomain(page, rows, sort, fullBizcode);
		else {
			switch (flowtype) {

			case "0":
				return super.getPagedFinishedFlowRows(page, rows, sort, fullBizcode);

			default:
				return null;
			}
		}

	}

	/**
	 * 分页查询用户列表页面
	 * 
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/flowList.action")
	public List<OALogisticsRepair> getPagedFlowRows(@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "rows", required = false) Integer rows,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "fullBizcode", required = false) String fullBizcode) {

		return super.getPagedFlowRows(page, rows, sort, fullBizcode);
	}

	/**
	 * 删除人员信息
	 * 
	 * @param id
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delRow(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "ids", required = false) String ids) {
		return super.delFlowRow(id, ids);

	}

	/**
	 * 保存缺陷记录
	 * 
	 * @param id
	 */
	@RequestMapping("/flowSave.action")
	public OALogisticsRepair save(@RequestBody String body) {

		OALogisticsRepair domain= super.saveOA(body);
		if((domain!=null) && (domain.getId()!=null)){
			domain = queryService.getOne(domain.getId());
		}
		
		//domain为空，返回空值
		return domain;
	}

	/**
	 * 保存缺陷记录
	 * 
	 * @param id
	 */

	public OALogisticsRepair autoSave(String userid, OALogisticsRepair obj) {

		OALogisticsRepair recipel = this.saveFlowEntity(userid, obj);
		if ((recipel != null) && (recipel.getId() != null)) {
			recipel = queryService.getOne(recipel.getId());
		}
		return recipel;
	}

	/**
	 * 签收记录
	 * 
	 * @param flowInstId
	 *            流程实例id
	 * @param flowTaskId
	 *            流程节点id
	 * @param signInfo
	 *            签收信息
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowSign.action")
	public ListDomain<Object> saveSingInfo(@RequestBody String body) {

		return this.saveSignInfo(shiroSecurityService.getCurrentUserid().toString(), body);
	}

	@Autowired
	private QueryOALogisticsRepairService queryService;

	@RequestMapping("/flowTaskComplete.action")
	public String completeTask(@RequestBody String body) {
		OALogisticsRepair recipel = this.save(body);
		if (recipel != null) {

			return super.completeTaskAndCandidateByEntity(shiroSecurityService.getCurrentUserid().toString(), recipel,
					"签收");
		}

		return "500";
	}

	/**
	 * 自动发送任务方法
	 * 
	 * @param userId
	 *            任务发送人员id
	 * @param domain
	 *            发送的业务单据实体
	 * @return
	 */
	public String completeTask(String userId, OALogisticsRepair domain) {
		domain = this.autoSave(userId, domain);

		if (domain != null && userId != null) {
			return super.completeTaskAndCandidateByEntity(userId, domain, "签收");
		}
		return "500";
	}

	@RequestMapping("/flowSendback.action")
	public RetDomain saveSendbackTask(@RequestBody String body) {
		OALogisticsRepair recipel = this.save(body);
		if (recipel != null) {
			return super.saveSendBack(shiroSecurityService.getCurrentUserid().toString(), recipel, null, null);

		}
		RetDomain domain = new RetDomain();
		domain.setErrorMsg("业务保存失败");
		return domain;
	}

	// 生成质询：从查询到保存，然后再发送
	public String finishLogisticsInquiry() {
		// null中应该是写一个sql语句
		ListDomain<OALogisticsRepair> logisticsPurchaseRepairList = getAllLogisticsInquireDomain();

		// for(OALogisticsPurchaseRepair obj:logisticsPurchaseRepairList){
		// OALogisticsPurchaseRepairService logisticsPurchaseRepairService = new
		// OALogisticsPurchaseRepairService();
		// logisticsPurchaseRepairService.autoSave(obj.getIds(), obj);
		// logisticsPurchaseRepairService.saveSingInfo(obj.toString());
		// }
		//

		return "";
	}

	public ListDomain<OALogisticsRepair> getAllLogisticsInquireDomain() {

		String sql = dicService.getSqlByBizcode("medical_inpatient_query");
		return queryService.getAllListDomain(sql, null, null);

	}

	// 从数据字典中获取sql查询语句
	public Long getBizCount(Map<String, String> paramMap) {
		// String userid = shiroSecurityService.getCurrentUserIdentify();
		// if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL,
		// "query_medical_all")){
		// String deptids = getUserDeptids(userid);
		// paramMap.put("doct_depart_code_in", deptids);
		// }
		String sql = dicService.getSqlByBizcode("medical_inpatient_query");

		return queryService.getBizCount(paramMap, sql, null, null);
		// return super.getBizCount(paramMap, sql, null, null);
	}

}